COMP3161/9164 Concepts of Programming Languages
Term 3, 2024

Tute (Week 1)

Table of Contents

1. Predicate Logic

1.1. Question 1

For which values of \(A\) and \(B\) does the boolean expression \(\varphi = \neg (A \Rightarrow B) \lor \neg B\) hold?

(todo)

1.2. Question 2

Simplify the boolean expression \((A \Rightarrow B) \lor (B \Rightarrow A)\)

(todo)

1.3. Question 3

A binary connective \(\bullet\) is defined as follows:

\(A\) \(B\) \(A\ \bullet\ B\)
\(\bot\) \(\bot\) \(\top\)
\(\bot\) \(\top\) \(\bot\)
\(\top\) \(\bot\) \(\top\)
\(\top\) \(\top\) \(\top\)

Restate the formula \(A \lor B\) in terms of \(\bullet\). What is the \(\bullet\) connective?

(todo)

1.4. Question 4

Assuming that \(F(x)\) states that the person \(x\) is my friend and that \(P(x)\) states that the person \(x\) is perfect, what is a logical translation of the phrase "None of my friends are perfect"?

(todo)

1.5. Question 5

Given the following function

\[ f (n) = \begin{cases} 0 & \text{if}\ n = 0 \\ 2n - 1 + f(n-1) & \text{if}\ n > 0 \end{cases} \]

Prove that \(\forall n \in \mathbb{N}.\ f(n) = n^2\).

(todo)

2. Haskell

Consider the Haskell code written in the lecture.

2.1. Haskell lexical structure

  1. What is the difference between a data and a type declaration?
  2. What is the difference between a type and a data constructor? List the identifiers in the code which represent type names, and those which represent data constructor names.
  3. Which phase of the compiler would be responsible for distinguishing type and data constructors?

(todo)

2.2. Haskell programming

  1. Write a Haskell function mySum that sums all elements in a list of numbers. Feel free to use the following template:

    mySum :: [Int] -> Int
    mySum []     = ???
    mySum (n:ns) = ???
    
  2. Write a Haskell function myProduct that multiplies all elements in a list of numbers.
  3. You probably used copypaste to solve the previous question, didn't you? No reason to be ashamed, we've all done it! But let's try not to.

    Find a generalisation myBinop that applies a given binary operator f with unit element z to a list of numbers. We will then be able to define myProduct and mySum using myBinop.

    myBinop :: (Int -> Int -> Int) -> Int -> ([Int] -> Int)
    myBinop f z [] = ???
    myBinop f z (n:ns) = ???
    
    mySum :: [Int] -> Int
    mySum ns = myBinop (+) 0 ns
    
    myProduct :: [Int] -> Int
    myProduct ns = myBinop (*) 1 ns
    
  4. We just reinvented a wheel. The fold functions are general-purpose library functions that completely subsume myBinop. Try to implement mySum and myProduct using foldr instead of myBinop.

    The linked library documentation references a lot of concepts that we don't assume familiarity with, so don't worry if you don't fully understand it. Perhaps start by looking at the examples.

(todo)

2024-11-28 Thu 20:06

Announcements RSS